package com.suray.basic.wcs.plc.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.suray.basic.wcs.plc.pojo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

public class StringBeanUtil {
    private static String NULL_FLAG = "$";
    public static String PRE_STR = "{";
    public static String SUF_STR = "}";
    public static String SPLIT_STR = ",";
    public static final int LIMIT_COUNT = 11;

    /**
     * 对象装换json发送到robot
     *
     * @param roBotHeartSendBean
     * @return
     */
    public static String toJson(RoBotHeartSendBean roBotHeartSendBean) {
        String json = StringUtils.EMPTY;
        SendMsgRobotHeartBeat sendMsgRobotHeartBeat = new SendMsgRobotHeartBeat();
        String no = roBotHeartSendBean.getNo();
        sendMsgRobotHeartBeat.setNo(no);
        CargoReady cargoReady = roBotHeartSendBean.getCargoReadyCommand().poll();
        if (cargoReady != null) {
            BeanUtils.copyProperties(cargoReady, sendMsgRobotHeartBeat);
        }
        TaskSend taskSend = roBotHeartSendBean.getTaskSendCommand().poll();
        if (taskSend != null) {
            BeanUtils.copyProperties(taskSend, sendMsgRobotHeartBeat);
        }
        TaskReceive taskReceive = roBotHeartSendBean.getTaskReceiveCommand().poll();
        if (taskReceive != null) {
            BeanUtils.copyProperties(taskReceive, sendMsgRobotHeartBeat);
        }
        json = JSON.toJSONString(sendMsgRobotHeartBeat, SerializerFeature.WriteMapNullValue);
        return json;
    }

    public static String toString(RoBotHeartSendBean roBotHeartSendBean) {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append(roBotHeartSendBean.getNo());
        sb.append(",");
        CargoReady cargoReady = roBotHeartSendBean.getCargoReadyCommand().poll();
        if (cargoReady != null) {
            sb.append(cargoReady.getCargoReadyTaskNo() != null ? cargoReady.getCargoReadyTaskNo() : NULL_FLAG);
            sb.append(",");
            sb.append(StringUtils.isNotBlank(cargoReady.getStationNo()) ? cargoReady.getStationNo() : NULL_FLAG);
            sb.append(",");
            sb.append(cargoReady.getPalletType() != null ? cargoReady.getPalletType() : NULL_FLAG);
            sb.append(",");
            sb.append(cargoReady.getCargoType() != null ? cargoReady.getCargoType() : NULL_FLAG);
            sb.append(",");
            sb.append(cargoReady.getCount() != null ? cargoReady.getCount() : NULL_FLAG);
            sb.append(",");
        } else {
            sb.append(NULL_FLAG);
            sb.append(",");
            sb.append(NULL_FLAG);
            sb.append(",");
            sb.append(NULL_FLAG);
            sb.append(",");
            sb.append(NULL_FLAG);
            sb.append(",");
            sb.append(NULL_FLAG);
            sb.append(",");
        }
        TaskSend taskSend = roBotHeartSendBean.getTaskSendCommand().poll();
        if (taskSend != null) {
            sb.append(taskSend.getTaskSendTaskNo() != null ? taskSend.getTaskSendTaskNo() : NULL_FLAG);
            sb.append(",");
            sb.append(taskSend.getProductType() != null ? taskSend.getProductType() : NULL_FLAG);
            sb.append(",");
            sb.append(StringUtils.isNotBlank(taskSend.getStartStationNo()) ? taskSend.getStartStationNo() : NULL_FLAG);
            sb.append(",");
            sb.append(StringUtils.isNotBlank(taskSend.getEndStationNo()) ? taskSend.getEndStationNo() : NULL_FLAG);
            sb.append(",");
            sb.append(taskSend.getCargoCount() != null ? taskSend.getCargoCount() : NULL_FLAG);
            sb.append(",");
            sb.append(taskSend.getTaskId() != null ? taskSend.getTaskId() : NULL_FLAG);
            sb.append(",");
        } else {
            sb.append(NULL_FLAG);
            sb.append(",");
            sb.append(NULL_FLAG);
            sb.append(",");
            sb.append(NULL_FLAG);
            sb.append(",");
            sb.append(NULL_FLAG);
            sb.append(",");
            sb.append(NULL_FLAG);
            sb.append(",");
            sb.append(NULL_FLAG);
            sb.append(",");
        }
        TaskReceive taskReceive = roBotHeartSendBean.getTaskReceiveCommand().poll();
        if (taskReceive != null) {
            sb.append(taskReceive.getWcsTaskNo() != null ? taskReceive.getWcsTaskNo() : NULL_FLAG);
            sb.append(",");
            sb.append(taskReceive.getStatus() != null ? taskReceive.getStatus() : NULL_FLAG);
        } else {
            sb.append(NULL_FLAG);
            sb.append(",");
            sb.append(NULL_FLAG);
        }
        sb.append("}");
        return sb.toString();
    }

    public static RobotHeartBaseResult toRobotHeartBaseResult(String receiveStr) {
        // 去头去尾
        String result = receiveStr.replace(StringBeanUtil.PRE_STR, "").replace(StringBeanUtil.SUF_STR, "");
        RobotHeartBaseResult robotHeartBaseResult = new RobotHeartBaseResult();
        String[] receiveStrArray = result.split(SPLIT_STR);
        robotHeartBaseResult.setNo(receiveStrArray[0]);
        robotHeartBaseResult.setStatus(NULL_FLAG.equals(receiveStrArray[1]) ? null : Integer.valueOf(receiveStrArray[1]));
        robotHeartBaseResult.setCargoReadyTaskNo(NULL_FLAG.equals(receiveStrArray[2]) ? null : Integer.valueOf(receiveStrArray[2]));
        robotHeartBaseResult.setCargoReadyStatus(NULL_FLAG.equals(receiveStrArray[3]) ? null : Integer.valueOf(receiveStrArray[3]));
        robotHeartBaseResult.setTaskSendTaskNo(NULL_FLAG.equals(receiveStrArray[4]) ? null : Integer.valueOf(receiveStrArray[4]));
        robotHeartBaseResult.setTaskSendStatus(NULL_FLAG.equals(receiveStrArray[5]) ? null : Integer.valueOf(receiveStrArray[5]));
        robotHeartBaseResult.setWcsTaskNo(NULL_FLAG.equals(receiveStrArray[6]) ? null : Integer.valueOf(receiveStrArray[6]));
        robotHeartBaseResult.setWcsTaskStatus(NULL_FLAG.equals(receiveStrArray[7]) ? null : Integer.valueOf(receiveStrArray[7]));
        robotHeartBaseResult.setStationNo(NULL_FLAG.equals(receiveStrArray[8]) ? null : Integer.valueOf(receiveStrArray[8]));
        robotHeartBaseResult.setStationStatus(NULL_FLAG.equals(receiveStrArray[9]) ? null : Integer.valueOf(receiveStrArray[9]));
        robotHeartBaseResult.setCargoCode(NULL_FLAG.equals(receiveStrArray[10]) ? null : receiveStrArray[10]);
        return robotHeartBaseResult;
    }

    public static void main(String[] args) {
        RoBotHeartSendBean roBotHeartSendBean = new RoBotHeartSendBean("robot_1");
        System.out.println(toString(roBotHeartSendBean));
        CargoReady cargoReady = new CargoReady("1号工位", 0, 1, 2);
        cargoReady.setCargoReadyTaskNo(201);
        roBotHeartSendBean.getCargoReadyCommand().offer(cargoReady);
        TaskSend taskSend = new TaskSend(3, 1, "1号工位", "2号工位", 2);
        taskSend.setTaskSendTaskNo(202);
        roBotHeartSendBean.getTaskSendCommand().offer(taskSend);
        TaskReceive taskReceive = new TaskReceive(101, 1);
        roBotHeartSendBean.getTaskReceiveCommand().offer(taskReceive);
        System.out.println(toString(roBotHeartSendBean));
        String receiveStr = "{robot_1,$,$,$,$,$,$,$,$}";
        System.out.println(toRobotHeartBaseResult(receiveStr));
        receiveStr = "{robot_1,1,201,0,202,1,10001,0,1}";
        System.out.println(toRobotHeartBaseResult(receiveStr));
    }
}
